package com.wang.interview.designmode.proxy.cglib;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author: wang
 * @date: 2021/8/1 17:20
 * @description:
 */
public class DynamicCglib {

    public <T> T getProxy(Class<T> clazz) {
        return (T)Enhancer.create(clazz, new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("开始代理");
                Object invoke = methodProxy.invokeSuper(o, objects);
                System.out.println("结束代理");
                return invoke;
            }
        });
    }
 }
